package com.rongshu.view.home.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.contract.MoreArticleDetailView;
import com.rongshu.view.home.entity.ArticleAttribute;
import com.rongshu.view.home.entity.ArticleDetails;
import com.rongshu.view.home.entity.ArticleItem;
import com.rongshu.view.home.entity.ArticleLike;
import com.rongshu.view.home.entity.UserCollect;

import java.util.List;

import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Func1;

public class MoreArticleDetailsPresenter extends BasePresenter<MoreArticleDetailView> {

    public MoreArticleDetailsPresenter(Context context) {
        super(context);
    }

    /**
     * 文章详情
     *
     * @param user_id
     * @param pageIndex
     * @param pageSize
     * @param sign
     */
    public void getArticleDetails(int user_id, int pageIndex, int pageSize, String sign) {
        RetorfitService.getPostDetail(user_id, pageIndex, pageSize, sign)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .map(new Func1<ArticleDetails, List<ArticleItem>>() {
                    @Override
                    public List<ArticleItem> call(ArticleDetails articleDetails) {
                        return articleDetails.getResponse();
                    }
                })
                .map(new Func1<List<ArticleItem>, List<ArticleItem>>() {
                    @Override
                    public List<ArticleItem> call(List<ArticleItem> articleItems) {
                        for (ArticleItem articleItem : articleItems) {
                            if (articleItem != null) {
                                int serialize_id = articleItem.getSerialize_id();
                                if (serialize_id > 0) {
                                    articleItem.setSerial(true);
                                }
                            }
                        }
                        return articleItems;
                    }
                })
                .compose(getView().<List<ArticleItem>>bindToLife())
                .subscribe(new Subscriber<List<ArticleItem>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<ArticleItem> responseBeans) {
                        if (getView() != null) {
                            getView().onArticleDetail(responseBeans);
                        }
                    }
                });
    }

    /**
     * 获取文章属性
     */
    public void getArticleAttribute(int user_id, int contribute_id, String sign) {
        RetorfitService.getArticleAttribute(user_id, contribute_id, sign)
                .compose(getView().<ArticleAttribute>bindToLife())
                .subscribe(new Subscriber<ArticleAttribute>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleAttribute articleAttribute) {
                        if (getView() != null) {
                            getView().onArticleAttribute(articleAttribute);
                        }
                    }
                });
    }

    /**
     * 离开文章时填写对文章的行为记录
     */
    public void getLogUserBehavior(int user_id, int contribute_id, int ScrollingPercentMax, int TotalStayTime, String sign) {
        RetorfitService.getLogUserBehavior(user_id, contribute_id, ScrollingPercentMax, TotalStayTime, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        if (getView() != null) {
                            getView().onUserBehavior(bean);
                        }
                    }
                });
    }

    /**
     * 浏览
     */
    public void getbrowse(int user_id, int contribute_id, int type, String code, String sign) {
        RetorfitService.getbrowse(user_id, contribute_id, type, code, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        if (getView() != null) {
                            getView().onBrowseArticle(bean);
                        }
                    }
                });
    }

    /**
     * 获取我是否收藏
     */
    public void getIsShouChang(int user_id, int contribute_id, int type, String sign) {
        RetorfitService.getIsShouChang(user_id, contribute_id, type, sign)
                .compose(getView().<UserCollect>bindToLife())
                .subscribe(new Subscriber<UserCollect>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserCollect userCollect) {
                        if (getView() != null) {
                            getView().onIsConcern(userCollect);
                        }
                    }
                });
    }

    /**
     * 点击收藏/点击取消
     */
    public void getIsCollect(int user_id, int type, int content_id, String sign) {
        RetorfitService.getIsCollect(user_id, type, content_id, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        if (getView() != null) {
                            getView().onIsCollect(iNetBean);
                        }
                    }
                });
    }

    /**
     * 文章点赞/帖子点赞
     */
    public void getlike(int id, int user_id, String type, String sign) {
        RetorfitService.getlike(id, user_id, type, sign)
                .compose(getView().<ArticleLike>bindToLife())
                .subscribe(new Subscriber<ArticleLike>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleLike articleLike) {
                        if (getView() != null) {
                            getView().onArticleLikeOrOffLike(articleLike, true);
                        }
                    }
                });
    }

    /**
     * 取消文章点赞
     */
    public void getOfflike(int user_id, String type, int contribute_id, String sign) {
        RetorfitService.getOfflike(user_id, type, contribute_id, sign)
                .compose(getView().<ArticleLike>bindToLife())
                .subscribe(new Subscriber<ArticleLike>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleLike articleLike) {
                        if (getView() != null) {
                            getView().onArticleLikeOrOffLike(articleLike, false);
                        }
                    }
                });
    }
}
